package com.joney.backstage.controller;

import com.joney.common.annotation.Auth;
import com.joney.backstage.constant.Permissions;
import com.joney.backstage.constant.ReqURLs;
import com.joney.backstage.dto.ResAddReq;
import com.joney.backstage.dto.ResEditReq;
import com.joney.backstage.entity.Resource;
import com.joney.backstage.service.ResourceService;
import com.joney.backstage.service.RoleService;
import com.joney.common.pojo.Result;
import com.joney.common.utils.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
* @author wangxianchen
* @create 2018-01-15
* @desc 角色控制器
*/
@Controller
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private RoleService roleService;

    @GetMapping(ReqURLs.RES_MGR)
    @Auth(Permissions.SYS_RES_VIEW)
    public String index(ModelMap model) {
        model.put("authMap",resourceService.getMgrPermission(JwtToken.getLoginUserId().intValue()));
        return "sys_base/res_mgr";
    }

    @GetMapping(ReqURLs.RES_TREE)
    @ResponseBody
    @Auth(Permissions.SYS_RES_VIEW)
    public Object tree(@RequestParam(defaultValue = "0") int pId,
                           @RequestParam(defaultValue = "0") int filterId) {
        return resourceService.getResTree(pId,filterId);
    }

    @GetMapping(ReqURLs.RES_AUTH_TREE)
    @ResponseBody
    @Auth(Permissions.SYS_RES_VIEW)
    public Object authTree(@RequestParam int roleId) {
        return resourceService.getAuthTree(roleId);
    }


    @PostMapping(ReqURLs.RES_INSERT)
    @ResponseBody
    @Auth(Permissions.SYS_RES_ADD)
    public Result insert(@RequestBody @Validated ResAddReq params) {
        return resourceService.addNode(params,JwtToken.getLoginUserId().intValue());
    }
    @GetMapping(ReqURLs.RES_REMOVE)
    @ResponseBody
    @Auth(Permissions.SYS_RES_DEL)
    public Result remove(@RequestParam String ids) {
        Result ret = new Result();
        resourceService.deleteNodes(ids);
        return ret;
    }

    @GetMapping(ReqURLs.RES_EDIT)
    @Auth(Permissions.SYS_RES_VIEW)
    public String editView(@RequestParam int id,ModelMap model) {
        Resource node = resourceService.selectById(id);
        model.put("node",node);
        model.put("parentNode",resourceService.selectById(node.getParentId()));
        return "sys_base/res_edit";
    }

    @GetMapping(ReqURLs.RES_DETAIL)
    @ResponseBody
    @Auth(Permissions.SYS_RES_VIEW)
    public Result detail(@RequestParam int id) {
        Resource node = resourceService.selectById(id);
        Result ret = new Result();
        Map<String,Object> map = new HashMap<>();
        map.put("node",node);
        map.put("parentNode",resourceService.selectById(node.getParentId()));
        map.put("roleList",roleService.selectRolesByResId(id));
        ret.setData(map);
        return ret;
    }

    @PostMapping(ReqURLs.RES_UPDATE)
    @ResponseBody
    @Auth(Permissions.SYS_RES_EDIT)
    public Result update(@RequestBody @Validated ResEditReq params) {
        return resourceService.updateNode(params,JwtToken.getLoginUserId().intValue());
    }
    @GetMapping(ReqURLs.RES_MOVE)
    @ResponseBody
    @Auth(Permissions.SYS_RES_EDIT)
    public Result move(@RequestParam int pid, @RequestParam int id) {
        return  resourceService.moveNote(pid,id,JwtToken.getLoginUserId().intValue());
    }
}
